home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 957 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: soap.news.pipex.net!pipex!usenet
  2. From: m.hendry@dial.pipex.com (Mathew Hendry)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Help with an easy C issue.
  5. Date: Sat, 13 Jan 96 04:06:18
  6. Organization: Private node.
  7. Distribution: world
  8. Message-ID: <19960113.44C1D0.3D32@ak056.du.pipex.com>
  9. References: <737.6585T1284T2078@in.net>
  10. NNTP-Posting-Host: ak056.du.pipex.com
  11. X-Newsreader: TIN [AMIGA 1.3 950726BETA PL0]
  12.  
  13. John J. Maver, Jr. (mave@in.net) wrote:
  14. :     I have a value entered as a string like,
  15. :     char *mystring;
  16. :     printf("Enter your age>");
  17. :     scanf("%s",&mystring);
  18. :     Now I want it to be an int, so:
  19. :     myint=atoi(mystring);
  20. :     I mess around with the int:
  21. :     myint=myint+10;
  22. :     And I want to turn myint back into a string.
  23. :     HOW???
  24.  
  25. Hey, even I can answer this ;)
  26.  
  27. The following is an example from Kernighan and Ritchie.
  28.  
  29. /* itoa: convert n to characters in s */
  30. void itoa(int n, char s[])
  31. {
  32.    int i, sign;
  33.  
  34.    if ((sign = n) < 0)
  35.       n = -n;
  36.    i = 0;
  37.    do {
  38.       s[i++] = n % 10 + '0';
  39.    } while (( n /= 10) > 0);
  40.    if (sign < 0)
  41.       s[i++] = '-';
  42.    s[i] = '\0';
  43.    reverse(s);
  44. }
  45.  
  46. Not the fastest way of going about things, and there may be something similar
  47. functions in your compiler's libraries, but there it is anyway.
  48.  
  49. -- Mat.
  50.